<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/11/1 0001
 * Time: 9:49
 */

namespace app\admin\controller;


use think\App;
use think\Controller;

class About extends Controller
{
    public function index(){
        $about = db("about")->select();
        $this->assign("about",$about);
        return view("index");
    }
    public function add(){
        if(request()->isAjax()){
            $param = request()->param();
            $file = request()->file('image');
            $info=$file->move( './static/images');
            $filename=str_replace("\\","/",$info->getSaveName());
            $image = '/static/images/'.$filename;
//            return $param;
            $data = [
                'name' => $param['name'],
                'title' => $param['title'],
                'descr' => $param['descr'],
                'content' => $param['content'],
                'image' => $image
            ];
            $add = db("about")->insert($data);
            if($add){
                return 200;
            }else{
                return -1;
            }
        }
        return view("add");
    }
    //详情
    public function detail(){
        $id = input("id");
        $data = db("about")->find($id);
        $this->assign("content",$data['content']);
        return view("detail");
    }
    public function delete(){
        $id  = input("id");
        $del = db("about")->delete($id);
        if($del){
            return 200;
        }else{
            return -1;
        }
    }
    public function edit()
    {
        if(request()->isAjax()){
            $id = input("id");
            $param = request()->except("id");
            $file = request()->file('image');
            if(!empty($file)){
                $info=$file->move( './static/images');
                $filename=str_replace("\\","/",$info->getSaveName());
                $image = '/static/images/'.$filename;
                $data = [
                    'name' => $param['name'],
                    'title' => $param['title'],
                    'descr' => $param['descr'],
                    'content' => $param['content'],
                    'image' => $image
                ];
            }else{
                $data = [
                    'name' => $param['name'],
                    'title' => $param['title'],
                    'descr' => $param['descr'],
                    'content' => $param['content'],
                    'image' => $param['old_image']
                ];
            }
            $res = db("about")->where("id",$id)->update($data);
            if($res){
                return 200;
            }else{
                return -1;
            }
        }
        $id = input("id");
        $data = db("about")->find($id);
        $this->assign("data",$data);
        return view("edit");
    }
    public function menu(){
        $menu = db("about_menu")->select();
        $this->assign("menu",$menu);
        return view("menu");
    }
    public function menu_add(){
        if(request()->isAjax()){
            $file = request()->file('image');
            $info=$file->move( './static/images');
            $filename=str_replace("\\","/",$info->getSaveName());
            $image = '/static/images/'.$filename;
            $param = request()->except("image");
            $data =  [
                'title' => $param['title'],
                'descr' => $param['descr'],
                'image' => $image,
                'addtime' => date("Y-m-d H:i:s",time())
            ];
            $add = db("about_menu")->insert($data);
            if($add){
                return 200;
            }else{
                return -1;
            }
        }
        return view("menu_add");
    }
    public function menu_edit(){
        if(request()->post()){
            $id = input("id");
            $param = request()->except("id");
            $file = request()->file('image');
            if(empty($file)){
                $data = [
                    'title' => $param['title'],
                    'descr' => $param['descr'],
                    'image' => $param['old_image'],
                ];
            }else{
                $info=$file->move( './static/images');
                $filename=str_replace("\\","/",$info->getSaveName());
                $image = '/static/images/'.$filename;
                $data = [
                    'title' => $param['title'],
                    'descr' => $param['descr'],
                    'image' => $image,
                ];
            }
            $update = db("about_menu")->where("id",$id)->update($data);
            if($update){
                return 200;
            }else{
                return -1;
            }
        }
        $id = input("id");
        $menu = db("about_menu")->find($id);
        $this->assign("menu",$menu);
        return view("menu_edit");
    }
    public function menu_del(){
        $id = input("id");
        $del = db("about_menu")->delete($id);
        if($del){
            return 200;
        }else{
            return -1;
        }
    }
}